package org.example.lys.stream;

import org.example.lys.json.JsonUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author liyishan
 * @date 2023/6/21 14:05
 * @apiNote 取对象列表的某个属性的集合
 */

public class test02 {
    public static void main(String[] args) {
        List<User> userList = build();
        test21(userList);
    }

    /**
     * List<User> userList
     *
     * @param userList List<Integer> ids
     *                 取对象列表的某个属性的集合
     */
    private static void test21(List<User> userList) {
        List<String> ids = userList.stream().map(User::getName)
                .filter(id -> id != null && !id.isEmpty())
                .collect(Collectors.toList());
        System.out.println(JsonUtils.toJson(ids));
    }

    /**
     * List<User> userList
     *
     * @param userList Set<Integer> collect
     *                 取出对象列表的某个属性的set
     */
    private static void test3(List<User> userList) {
        Set<Integer> collect = userList.stream().map(User::getId).collect(Collectors.toSet());
        System.out.println(JsonUtils.toJson(collect));
    }


    /**
     * List<User> userList
     *
     * @param userList Set<Integer> collect
     *                 取出对象列表的某个属性的set   可以修改
     */
    private static void test4(List<User> userList) {
        Set<Integer> collect = userList.stream().map(user -> {
            Integer id = user.getId();
            return id;
        }).collect(Collectors.toSet());
        System.out.println(JsonUtils.toJson(collect));
    }


    /**
     * 把对象列表的某个属性进行去重返回
     *
     * @param userList
     */
    private static void test9(List<User> userList) {
        List<BigDecimal> result = userList.stream().map(User::getPrice).distinct().collect(Collectors.toList());
        System.out.println(result);
    }


    /**
     * 获取某个属性并排序
     *
     * @param userList
     */
    private static void test13(List<User> userList) {
        List<BigDecimal> price = userList.stream().map(User::getPrice).sorted(BigDecimal::compareTo).collect(Collectors.toList());
        System.out.println(JsonUtils.toJson(price));
    }


    private static List<User> build() {
        List<User> userList = new ArrayList<>();
        User user1 = new User();
        user1.setId(1);
        //user1.setName("一");
        user1.setPrice(new BigDecimal("1.11"));
        userList.add(user1);
        User user2 = new User();
        user2.setId(2);
        //user2.setName("二");
        user2.setPrice(new BigDecimal("2.22"));
        userList.add(user2);
        User user3 = new User();
        user3.setId(3);
        //user3.setName("三");
        user3.setPrice(new BigDecimal("3.33"));
        userList.add(user3);
        User user31 = new User();
        user31.setId(4);
        //user31.setName("三");
        user31.setPrice(new BigDecimal("3.33"));
        userList.add(user31);
        return userList;
    }

}
